package com.example.alilist.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class LettersBar extends View {

    public static final String[] sLetters = { "A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
            "T", "U", "V", "W", "X", "Y", "Z", "#" };

    private static final int sAdjusVer = 20;

    private int mChoose = -1;
    private Paint mPaint = new Paint();

    private OnLetterChangedListener mOnLetterChangedListener;
    private TextView mLetterView;

    public void setTextView(TextView mLetterView) {
        this.mLetterView = mLetterView;
    }

    public LettersBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LettersBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LettersBar(Context context) {
        super(context);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight() - sAdjusVer * 2;
        int width = getWidth();
        int singleHeight = height / sLetters.length;
        
        float measure;
        float xPos;
        float yPos;
        
        for (int i = 0; i < sLetters.length; i++) {
            mPaint.setColor(Color.rgb(33, 65, 98));
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(30);
            if (i == mChoose) {
                mPaint.setColor(Color.parseColor("#3399ff"));
                mPaint.setFakeBoldText(true);
            }
             measure = mPaint.measureText(sLetters[i]);
             xPos = (width - measure) / 2;
             yPos = singleHeight * i + ((singleHeight - measure) / 2) + sAdjusVer;
            canvas.drawText(sLetters[i], xPos, yPos, mPaint);
            mPaint.reset();
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = mChoose;
        final OnLetterChangedListener listener = mOnLetterChangedListener;
        final int c = (int) ((y - sAdjusVer) / (getHeight() - sAdjusVer * 2)  * sLetters.length);

        switch (action) {
        case MotionEvent.ACTION_UP:
            mChoose = -1;//
            invalidate();
            if (mLetterView != null) {
                mLetterView.setVisibility(View.INVISIBLE);
            }
            break;

        default:
            if (oldChoose != c) {
                if (c >= 0 && c < sLetters.length) {
                    if (listener != null) {
                        listener.onLetterChanged(sLetters[c]);
                    }
                    if (mLetterView != null) {
                        mLetterView.setText(sLetters[c]);
                        mLetterView.setVisibility(View.VISIBLE);
                    }

                    mChoose = c;
                    invalidate();
                }
            }

            break;
        }
        return true;
    }

    /**
     * 
     * @param OnLetterChangedListener
     */
    public void setOnLetterChangedListener(OnLetterChangedListener listener) {
        this.mOnLetterChangedListener = listener;
    }

    public interface OnLetterChangedListener {
        public void onLetterChanged(String letter);
    }

}